'use strict';
const {
    Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
    class Product extends Model {
        /**
         * Helper method for defining associations.
         * This method is not a part of Sequelize lifecycle.
         * The `models/index` file will call this method automatically.
         */
        static associate(models) {
            // 商品属于分类
            models.Product.belongsTo(models.Category, {as: 'category'})
            // 商品属于品牌
            models.Product.belongsTo(models.Brand, {as: 'brand'})

            // 商品与标签多对多
            models.Product.belongsToMany(models.Tag, {as: 'tags', through: "ProductTag", foreignKey: "productId"})

            // 商品有很多相册
            models.Product.hasMany(models.ProductGallery, {as: 'galleries'})
        }
    }

    Product.init({
        name: DataTypes.STRING,
        categoryId: DataTypes.INTEGER,
        brandId: DataTypes.INTEGER,
        image: DataTypes.STRING,
        price: DataTypes.DECIMAL,
        stock: DataTypes.INTEGER,
        sale: DataTypes.INTEGER,
        onsale: DataTypes.BOOLEAN,
        recommend: DataTypes.BOOLEAN,
        discount: DataTypes.BOOLEAN,
        emergency: DataTypes.BOOLEAN,
        nourishing: DataTypes.BOOLEAN,
        prevention: DataTypes.BOOLEAN,
        commonUse: DataTypes.BOOLEAN,
        cardiovascular: DataTypes.BOOLEAN,
        desc: DataTypes.STRING,
        specs: DataTypes.STRING,
        unit: DataTypes.STRING,
        approval_number: DataTypes.STRING,
        factory: DataTypes.STRING,
        trademark: DataTypes.STRING,
        place_of_origin: DataTypes.STRING,
        classification: DataTypes.STRING,
        drug: DataTypes.STRING,
        expiry_date: DataTypes.STRING,
        reminder: DataTypes.TEXT,
    }, {
        sequelize,
        modelName: 'Product',
    });
    return Product;
};
